"""
Case Type   : CM_Tools
Case Name   : cm_ctl 公共参数验证
Create At   : 2025/5/21
Owner       : ningyali
Description :
    1.-n nodeid,指定节点，-D参数，指定实例目录,-t secs 指定超时时间
    2.-z availability_zone，指定az名称
    3.-l filename, 将结果输出到指定文件
    4.查看帮助信息
    5.查看版本信息
    6.超时时间内stop失败,查看集群状态
Expect      :
    1.停止指定实例在超时时间内成功
    2.执行成功
    3.查看节点状态信息成功，并将打印信息输出到指定文件中(不会输出在终端)
    4.执行成功
    5.执行成功
    6.stop执行失败(超时时间内执行失败，但后台命令仍在执行)，查询状态-n节点停
History     :
"""

import os
import time
import unittest

from testcase.utils.ComInstall import CmInstall
from testcase.utils.ComInstall import CmUnInstall
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class CMServer(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.root_nodes = (Node('PrimaryRoot'), Node('Standby1Root'),
                           Node('Standby2Root'))
        self.user_nodes = (Node('PrimaryDbUser'), Node('Standby1DbUser'),
                           Node('Standby2DbUser'))
        self.common = Common()
        self.constant = Constant()
        self.q_log_path = os.path.join(macro.DB_BACKUP_PATH, 'cm_query.log')

        text = '----prestep1:安装cm;expect:执行成功----'
        self.log.info(text)
        self.install_pkg_path = os.path.dirname(macro.DB_SCRIPT_PATH)
        self.cm = CmInstall(self.install_pkg_path, self.pri_sh.node.ssh_user)
        res = self.cm.update_xml_for_cm(*self.root_nodes)
        self.assertGreater(res.count('cmDir'), 1, f'执行失败: {text}')

        res = self.cm.cm_install(self.user_nodes[0])
        self.assertIn('Install CM tool success', res, f'执行失败: {text}')

    def test_trans(self):
        text = '----step1:-n nodeid,指定节点，-D参数，指定实例目录,-t secs 指定超时时间;' \
               'expect:停止指定实例在超时时间内成功----'
        self.log.info(text)
        q_cmd = f"source {macro.DB_ENV_PATH};" \
                f"cm_ctl stop -n 2 -D {macro.DB_INSTANCE_PATH} -t 60"
        q_res = self.common.get_sh_result(self.user_nodes[0], q_cmd)
        self.assertIn('cm_ctl: stop instance successfully', q_res,
                      f'执行失败: {text}')

        text = '----step2:-z availability_zone，指定az名称;expect:执行成功----'
        self.log.info(text)
        q_cmd = f"source {macro.DB_ENV_PATH};" \
                f"cm_ctl stop -z AZ1"
        q_res = self.common.get_sh_result(self.user_nodes[0], q_cmd)
        self.assertIn('cm_ctl: stop availability zone successfully', q_res,
                      f'执行失败: {text}')

        text = '----step3:-l filename, 将结果输出到指定文件;' \
               'expect:查看节点状态信息成功，并将打印信息输出到指定文件中(不会输出在终端)----'
        self.log.info(text)
        q_cmd = f"source {macro.DB_ENV_PATH};" \
                f"cm_ctl start -z AZ1; sleep 10;" \
                f"cm_ctl query -v -C -i -d -l {self.q_log_path}"
        cat_cmd = f'cat {self.q_log_path}; rm -rf {self.q_log_path}'
        q_res = self.common.get_sh_result(self.user_nodes[0], q_cmd)
        cat_res = self.common.get_sh_result(self.user_nodes[0], cat_cmd)
        self.assertIn('cm_ctl: start availability zone successfully', q_res,
                      f'执行失败: {text}')
        self.assertNotIn(cat_res, q_res, f'执行失败: {text}')

        text = '----step4:查看帮助信息;expect:执行成功----'
        self.log.info(text)
        q_cmd1 = f"source {macro.DB_ENV_PATH};cm_ctl -?"
        q_cmd2 = f"source {macro.DB_ENV_PATH};cm_ctl --help"
        q_cmd3 = f"source {macro.DB_ENV_PATH};cm_ctl -h"
        q_res1 = self.common.get_sh_result(self.user_nodes[0], q_cmd1)
        q_res2 = self.common.get_sh_result(self.user_nodes[0], q_cmd2)
        q_res3 = self.common.get_sh_result(self.user_nodes[0], q_cmd3)
        self.assertTrue(q_res1 == q_res2 == q_res3, f'执行失败: {text}')

        text = '----step5:查看版本信息;expect:执行成功----'
        self.log.info(text)
        q_cmd1 = f"source {macro.DB_ENV_PATH};cm_ctl -V"
        q_cmd2 = f"source {macro.DB_ENV_PATH};cm_ctl --version"
        q_res1 = self.common.get_sh_result(self.user_nodes[0], q_cmd1)
        q_res2 = self.common.get_sh_result(self.user_nodes[0], q_cmd2)
        self.assertTrue(q_res1 == q_res2, f'执行失败: {text}')

        text = '----step6:超时时间内stop失败,查看集群状态;' \
               'expect:stop执行失败(超时时间内执行失败，但后台命令仍在执行)，查询状态-n节点停----'
        self.log.info(text)
        q_cmd = f"source {macro.DB_ENV_PATH};" \
                f"cm_ctl stop -n 2 -D {macro.DB_INSTANCE_PATH} -t 1;" \
                f"cm_ctl query -Civd"
        q_res = self.common.get_sh_result(self.user_nodes[0], q_cmd)
        self.assertIn('cm_ctl: stop instance failed in (1)s', q_res,
                      f'执行失败: {text}')

    def tearDown(self):
        time.sleep(10)
        text = '----poststep1:还原数据库;expect:执行成功----'
        self.log.info(text)
        q_cmd = f"source {macro.DB_ENV_PATH};" \
                f"cm_ctl query -Civdp && " \
                f"cm_ctl start -n 2 -D {macro.DB_INSTANCE_PATH} && " \
                f"sleep 10 && " \
                f"cm_ctl query -Civdp"
        q_res = self.common.get_sh_result(self.user_nodes[0], q_cmd)

        text = '----poststep2:卸载cm;expect:执行成功----'
        self.log.info(text)
        self.un_cm = CmUnInstall(self.install_pkg_path,
                                 self.pri_sh.node.ssh_user)
        res = self.un_cm.cm_uninstall(
            self.user_nodes[0], '--deleteData --deleteBinary')

        self.assertIn('cm_ctl: start instance successfully', q_res,
                      f'执行失败: {text}')
        self.assertIn('Uninstall CM tool success', res, f'执行失败: {text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
